package com.xspace.android.xafcommon.download;

import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextUtils;

import com.xspace.android.xafcommon.util.LogUtil;

import java.io.File;

public class DownloadInfo implements Parcelable {
    public LoadInfo req;
    public String saveFileName;   //Final file name
    public String tmpFileName;    //Temporary file name
    public long size = 0;

    public DownloadInfo(LoadInfo req) {
        super();
        this.req = req;



        this.saveFileName = (TextUtils.isEmpty(req.exsitDir) ? FileUtils.getPath() : req.exsitDir) + "/" + req.directory + "/" + req.filename + req.format;
        LogUtil.d("this.saveFileName" + this.saveFileName);
        this.tmpFileName = (TextUtils.isEmpty(req.exsitDir) ? FileUtils.DOWNLOAD_TEMP_PATH : req.exsitDir) + "/" + req.directory + "/" + req.filename + ".td";
        LogUtil.d("this.tmpFileName" + this.tmpFileName);
        this.size = 10;
        String downloadDir = (TextUtils.isEmpty(req.exsitDir) ? FileUtils.getPath() : req.exsitDir) + "/" + req.directory;
        LogUtil.d("downloadDir  " + downloadDir);
        if (!new File(downloadDir).exists()) {
            LogUtil.d("创建下载目录>>" + downloadDir);
            new File(downloadDir).mkdirs();
        }
    }

    public DownloadInfo(Parcel in) {
        readFromParcel(in);
    }

    protected void readFromParcel(Parcel in) {
        req = (LoadInfo) in.readSerializable();
        saveFileName = in.readString();
        tmpFileName = in.readString();
        size = in.readLong();
    }

    @Override
    public void writeToParcel(Parcel parcel, int flags) {
        parcel.writeSerializable(req);
        parcel.writeString(saveFileName);
        parcel.writeString(tmpFileName);
        parcel.writeLong(size);
    }

    @Override
    public int describeContents() {
        return 0;
    }

    public static final Parcelable.Creator<DownloadInfo> CREATOR =
            new Parcelable.Creator<DownloadInfo>() {
                public DownloadInfo createFromParcel(Parcel in) {
                    return new DownloadInfo(in);
                }

                public DownloadInfo[] newArray(int size) {
                    return new DownloadInfo[size];
                }
            };
}
